﻿using CefSharp;
using CefSharp.WinForms.Example;
using DevExpress.XtraBars;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Utils.Ini;

namespace MyCef
{
    public partial class MainForm : TabForm
    {
        #region  构造
        public MainForm() : this(true) { }
        public MainForm(bool hasPage)
        {
            InitializeComponent();
            Initialize(hasPage);
        }
        #endregion

        #region 基本属性
        private string defUrl = IniUtil.GetPrivateProfileString("broswer", "defaultUrl", "cfg.ini");

        private double zoomIncrement = Double.Parse(IniUtil.GetPrivateProfileString("broswer", "zoomIncrement", "cfg.ini"));

        private bool multiThreadedMessageLoopEnabled = Boolean.Parse(IniUtil.GetPrivateProfileString("broswer", "multiThreadedMessageLoopEnabled", "cfg.ini"));

        private bool shouldInitHomePage = true;

        bool HidePageCaption { get; set; }
        private bool skinProcessing = false;

        private string currentUrl = string.Empty;

        private IWindowInfo info;
        #endregion

        #region 私有方法
        void InitSkins()
        {
            barManager1.ForceInitialize();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="shouldCreateWebBrowser"></param>
        protected void Initialize(bool shouldCreateWebBrowser)
        {
            this.Text = "若祎";
            this.shouldInitHomePage = shouldCreateWebBrowser;
            if (shouldCreateWebBrowser)
            {
                CreatePageWebBrowser(defUrl, TabFormControl.SelectedPage);
                // SubscribeOnPageEvents();
            }
            else
            {
                TabFormControl.Pages.Clear();
                TabFormControl.SelectedPage = null;
            }
            TabFormControl.PageCreated += OnPageCreated;
            // Icon = DevExpress.Utils.ResourceImageHelper.CreateIconFromResourcesEx("BrowserDemo.AppIcon.ico", typeof(frmMain).Assembly);
            barManager1.ForceLinkCreate();
            barManager1.GetController().Changed += new EventHandler(ChangedController);
            InitSkins();
            this.Focus();
        }
        /// <summary>
        /// 页面样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChangedController(object sender, EventArgs e)
        {
            if (skinProcessing) return;
            string paintStyleName = barManager1.GetController().PaintStyleName;
            if ("DefaultSkin".IndexOf(paintStyleName) >= 0)
                DevExpress.Skins.SkinManager.EnableFormSkins();
            else DevExpress.Skins.SkinManager.DisableFormSkins();
            skinProcessing = true;
            DevExpress.LookAndFeel.LookAndFeelHelper.ForceDefaultLookAndFeelChanged();
            skinProcessing = false;
        }

        /// <summary>
        /// 创建一个新的页面
        /// </summary>
        /// <param name="url"></param>
        /// <param name="insertIndex"></param>
        private void CreatePageWebBrowser(string url, TabFormPage page)
        {
            String tabName = url;
            browserTabControl.SuspendLayout();
            var browser = new DevBrowserTabUserControl(url, multiThreadedMessageLoopEnabled)
            {
                Dock = DockStyle.Fill,
                tabFormPage = page
            };
            page.ContentContainer.Controls.Add(browser);
            browser.CreateControl();
            page.Text = tabName;
            page.Tag = url;


            browserTabControl.ResumeLayout();

        }
        #endregion

        #region 公开的方法
        public void CreatePageWebBrowser(string url, IWindowInfo windowInfo)
        {
            this.info = windowInfo;
            this.currentUrl = url;
            this.browserTabControl.AddNewPage();
        }
        #endregion

        #region  事件
        /// <summary>
        /// 创建一个新的页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnPageCreated(object sender, PageCreatedEventArgs e)
        {
            if (HidePageCaption)
            {
                e.Page.Text = string.Empty;
            }
            if (this.currentUrl != string.Empty)
            {
                var rect = e.Page.ContentContainer.ClientRectangle;
                this.info.SetAsChild(e.Page.ContentContainer.Handle, rect.Left, rect.Top, rect.Right, rect.Bottom);
            }
            else
            {
                this.currentUrl = defUrl;
            }

            this.CreatePageWebBrowser(this.currentUrl, e.Page);
            this.currentUrl = string.Empty;
            this.info = null;
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
        #endregion
    }
}
